<?php

/**
 * 支付宝支付
 * 
 * 
 * @author  LY
 * @version  版本号
 * @date  2019-04-28
 */

namespace app\erp\designpattern\pay;

/**
 * it618 实现类
 * 
 * 类的详细介绍（可选）
 * @author  作者
 * @version  版本号
 * @date  2020-02-02
 */
class Alipay implements PayInterface {
                          
    public $gatewayUrl = 'https://openapi.alipay.com/gateway.do'; 
    public $appId = 2015122301030715;
    public $format = "json";
    public $postCharset = "UTF-8";
    public $signType = "RSA2";
//    public $signType = "RSA";
//    支付宝私钥
    public $rsaPrivateKey = 'MIIEowIBAAKCAQEAtXJnEFItprZtZXUwp9R2GeZ68Jc+Cjj0gBVcUi8M3XB4iGkl9SjKEXDwnJV6akAflFDGKA+O/2iAeqXMdN0oPehAsKFrDVMjI1a3+FM+N25Dzw++LFjPoVZbrrD+gqZpfRZ/kcvxhmjmPFtUFP0BPd6DDNfREihtYP68sVWPtkn80uIytNN3156kmVRVCtSUQUdQW1IoLPxAtj5q431VYFokXaepeyKQAG2Ri4q8gE8fY1HkliosF6ZG1dyvzkY4H1QteG2R0+yKtp/rIHen/p2uzCtKSEj9/fA/8nvIyDaPTaFK411P/oWueRG2v2Adx68VBBmXuZpK1UkzP/rC+wIDAQABAoIBADfo0n0i9VIjcD4NCDsOqn18mXE9CS7ZMe2tp07jlJE/MMTHsZyDPxB0vo9hN+E5dQGcihKaCLQGnWAtanywENBSmmxSsMvgTDl9ZR6td8RYw6gv76d2jjPmikWf4Cyd3m9e2VwxfXCvLhJ3CceV1iYcREFW1uNDdcCnfbke5jOCv9t7ibxYkkjXPwMYuCHCVILRZ1D52Bi3VTZAWFiyOoeGx3n9EDhoFOkNe/kQxgk/c/xFM2jCaKUYoHjo4fxG8+2iLpJqIRbNV9mCbdbGNkqTuU5EXDK5Ojc+qUCQ4RZfBwhfOCXE3mcm+BbsVjeoStmTETwvxSwNMHKkUUIQpGECgYEA4l8wt6r553NR3pvvg6DVGGb/Sd9r7hUBKYEHqf5JvwPfEZRWmIZKKXzfIYzM6D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ZsfavGYomAJNPnFHG9hVirGqLeOjpOOHuYWZ7rgg1ULaaOZQPKzTUMWuyRbeLYUhiK4NKp7rM4Y7xv/U1GUMGcKSXpe8IDPgyL1HkmeeLMAWS3TS4ZWAoYS54wKd49s0I2EwNKyhrTOogLqXUi9pqg3ZQIDAQAB';
//    public $alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtXJnEFItprZtZXUwp9R2GeZ68Jc+Cjj0gBVcUi8M3XB4iGkl9SjKEXDwnJV6akAflFDGKA+O/2iAeqXMdN0oPehAsKFrDVMjI1a3+FM+N25Dzw++LFjPoVZbrrD+gqZpfRZ/kcvxhmjmPFtUFP0BPd6DDNfREihtYP68sVWPtkn80uIytNN3156kmVRVCtSUQUdQW1IoLPxAtj5q431VYFokXaepeyKQAG2Ri4q8gE8fY1HkliosF6ZG1dyvzkY4H1QteG2R0+yKtp/rIHen/p2uzCtKSEj9/fA/8nvIyDaPTaFK411P/oWueRG2v2Adx68VBBmXuZpK1UkzP/rC+wIDAQAB';

    /**
     * 预支付订单
     * @param string $body 支付说明
     * @param string $out_trade_no 订单号
     * @param int $total_fee 支付金额 分
     * @param string $notify_url 回调地址
     * @return 
     */
    public function getPrePayOrder($body, $out_trade_no, $total_fee, $notify_url) {
        Vendor('alyun-php-pay.AopSdk'); //引入文件

        $aop = new \AopClient;
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->format = $this->format;
        $aop->postCharset = $this->postCharset;
        $aop->signType = $this->signType;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();

        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $bizcontent = json_encode([
            'body' => $body,
            'subject' => $out_trade_no,
            'out_trade_no' => $out_trade_no,
            'timeout_express' => '30m',
            'total_amount' => round($total_fee / 100, 2),
            'product_code' => 'QUICK_MSECURITY_PAY',
        ]);

        $request->setNotifyUrl($notify_url);
        $request->setBizContent($bizcontent);

        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        echo '<pre>';
        var_dump($response);
        exit;
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        echo htmlspecialchars($response); //就是orderString 可以直接给客户端请求，无需再做处理。
    }

 

    public function getTest1($body, $out_trade_no, $total_fee, $notify_url) {
        Vendor('alyun-php-pay.AopSdk'); //引入文件
        $aop = new \AopClient ();


        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->format = $this->format;
//        $aop->postCharset = $this->postCharset;
        $aop->signType = $this->signType;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;

        $bizcontent = json_encode([
            'body' => $body,
            'subject' => $out_trade_no,
            'out_trade_no' => $out_trade_no,
            'timeout_express' => '30m',
            'total_amount' => round($total_fee / 100, 2),
        ]);
        $request = new \AlipayTradeAppPayRequest ();
        $request->setBizContent($bizcontent);
        $result = $aop->execute($request);
        echo '<pre>';
        var_dump($result);
        exit;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        
        $resultCode = $result->$responseNode->code;
        echo '<pre>';
        var_dump($responseNode);
        var_dump($resultCode);
        
       
    }
    
    
    
    

}
